package com.wanli.storemobile.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.wanli.storemobile.R;
import com.wanli.storemobile.base.DataCallBack;
import com.wanli.storemobile.bean.AreaListBean;
import com.wanli.storemobile.homepage.adapter.ChooseAreaListAdapter;
import com.wanli.storemobile.homepage.model.HomePageModelImpl;
import com.wanli.storemobile.homepage.model.IHomePageModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Author:HWC
 */
public class ChooseCityDialog extends DialogFragment {

    @BindView(R.id.view_provience)
    View viewProvience;
    @BindView(R.id.ll_provience)
    LinearLayout llProvience;
    @BindView(R.id.view_city)
    View viewCity;
    @BindView(R.id.ll_city)
    LinearLayout llCity;
    @BindView(R.id.view_area)
    View viewArea;
    @BindView(R.id.ll_area)
    LinearLayout llArea;
    @BindView(R.id.tv_provience_name)
    TextView tvProvienceName;
    @BindView(R.id.tv_city_name)
    TextView tvCityName;
    @BindView(R.id.tv_area_name)
    TextView tvAreaName;
    @BindView(R.id.recyclerView_provience)
    RecyclerView recyclerViewProvience;
    @BindView(R.id.recyclerView_city)
    RecyclerView recyclerViewCity;
    @BindView(R.id.recyclerView_area)
    RecyclerView recyclerViewArea;
    private Dialog dialog;
    private OnCallBackListener onCallBackListener;
    private Activity mActivity;
    private String provience;
    private String city;
    private String area;
    private IHomePageModel homePageModel = new HomePageModelImpl();
    private ChooseAreaListAdapter provienceAdapter;
    private ChooseAreaListAdapter cityAdapter;
    private ChooseAreaListAdapter areaAdapter;

    private List<AreaListBean.DataBean> provienceList = new ArrayList<>();
    private List<AreaListBean.DataBean> cityList = new ArrayList<>();
    private List<AreaListBean.DataBean> areaList = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        initWindow();
        View view = inflater.inflate(R.layout.dialog_select_city, null);
        ButterKnife.bind(this, view);
        mActivity = getActivity();
        initView();
        return view;
    }

    public static ChooseCityDialog newInstance(String provience, String city, String area) {
        Bundle args = new Bundle();
        ChooseCityDialog fragment = new ChooseCityDialog();
        args.putString("provience", provience);
        args.putString("city", city);
        args.putString("area", area);
        fragment.setArguments(args);
        return fragment;
    }

    public void setOnCallBackListener(OnCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }

    //    初始化请求
    private void initView() {
        provience = getArguments().getString("provience");
        city = getArguments().getString("city");
        area = getArguments().getString("area");

        provienceAdapter = new ChooseAreaListAdapter(provienceList, 1);
        cityAdapter = new ChooseAreaListAdapter(cityList, 2);
        areaAdapter = new ChooseAreaListAdapter(areaList, 3);

        recyclerViewProvience.setLayoutManager(new LinearLayoutManager(mActivity));
        recyclerViewProvience.setAdapter(provienceAdapter);

        recyclerViewCity.setLayoutManager(new LinearLayoutManager(mActivity));
        recyclerViewCity.setAdapter(cityAdapter);

        recyclerViewArea.setLayoutManager(new LinearLayoutManager(mActivity));
        recyclerViewArea.setAdapter(areaAdapter);


        provienceAdapter.setOnCallBackListener((name, code) -> {
            provience = name;
            tvProvienceName.setText(provience);
            requestGetCity(code);
            setDisplay(2);
        });

        cityAdapter.setOnCallBackListener((name, code) -> {
            city = name;
            tvCityName.setText(city);
            requestGetArea(code);
            setDisplay(3);
        });

        areaAdapter.setOnCallBackListener((name, code) -> {
            area = name;
            tvAreaName.setText(area);
            if (onCallBackListener != null) {
                onCallBackListener.onCallBack(provience, city, area);
            }
            dismiss();
        });

        if (!TextUtils.isEmpty(provience)) {
            tvProvienceName.setText(provience);
        }
        if (!TextUtils.isEmpty(city)) {
            tvCityName.setText(city);
        }
        if (!TextUtils.isEmpty(area)) {
            tvAreaName.setText(area);
        }

        requestGetProvince();
    }

    public void setDisplay(int type) {
        viewProvience.setVisibility(View.INVISIBLE);
        viewCity.setVisibility(View.INVISIBLE);
        viewArea.setVisibility(View.INVISIBLE);
        recyclerViewProvience.setVisibility(View.GONE);
        recyclerViewCity.setVisibility(View.GONE);
        recyclerViewArea.setVisibility(View.GONE);
        if (type == 1) {
            viewProvience.setVisibility(View.VISIBLE);
            recyclerViewProvience.setVisibility(View.VISIBLE);
        }
        if (type == 2) {
            viewCity.setVisibility(View.VISIBLE);
            recyclerViewCity.setVisibility(View.VISIBLE);
        }
        if (type == 3) {
            viewArea.setVisibility(View.VISIBLE);
            recyclerViewArea.setVisibility(View.VISIBLE);
        }
    }

    public void requestGetProvince() {
        homePageModel.requestGetProvince(new DataCallBack<AreaListBean>() {
            @Override
            public void onSuccessful(AreaListBean bean) {
                provienceList.clear();
                provienceList.addAll(bean.getData());
                provienceAdapter.notifyDataSetChanged();
                if (!TextUtils.isEmpty(provience)) {
                    for (AreaListBean.DataBean dataBean : provienceList) {
                        if (provience.equals(dataBean.getProvince_name())) {
                            requestGetCity(dataBean.getProvince_code());
                            break;
                        }
                    }
                }
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    public void requestGetCity(int province_code) {
        homePageModel.requestGetCity(province_code, new DataCallBack<AreaListBean>() {
            @Override
            public void onSuccessful(AreaListBean bean) {
                cityList.clear();
                cityList.addAll(bean.getData());
                cityAdapter.notifyDataSetChanged();

                if (!TextUtils.isEmpty(city)) {
                    for (AreaListBean.DataBean dataBean : cityList) {
                        if (city.equals(dataBean.getCity_name())) {
                            requestGetArea(dataBean.getCity_code());
                            break;
                        }
                    }
                }
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    public void requestGetArea(int city_code) {
        homePageModel.requestGetArea(city_code, new DataCallBack<AreaListBean>() {
            @Override
            public void onSuccessful(AreaListBean bean) {
                areaList.clear();
                areaList.addAll(bean.getData());
                areaAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    //设置window属性
    private void initWindow() {
        //去掉dialog的标题
        dialog = this.getDialog();
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(true);
        Window window = dialog.getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        window.getAttributes().windowAnimations = R.style.commomDialog;
        window.setAttributes(lp);
        window.setBackgroundDrawable(new ColorDrawable());
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @OnClick({R.id.ll_provience, R.id.ll_city, R.id.ll_area})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_provience:
                setDisplay(1);
                break;
            case R.id.ll_city:
                setDisplay(2);
                break;
            case R.id.ll_area:
                setDisplay(3);
                break;
        }
    }

    public interface OnCallBackListener {
        void onCallBack(String provience, String city, String area);
    }
}
